package com.devilmole.vote.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by devilmole on 2017/8/31.
 */
@Controller
@Log4j2
public class FileController {


    @RequestMapping(method = {RequestMethod.GET},value = "/getFile")
    public void getFile(HttpServletResponse response){
        response.setContentType("image/jpeg");
        String fileName="ceshi.jpg";
        try {
            response.setHeader("Content-disposition", "attachment; filename=\""+java.net.URLEncoder.encode(fileName, "UTF-8")+"\"");
            File file=new File("/Users/devilmole/Documents/attachment/1901726036.jpg");
            FileCopyUtils.copy(new FileInputStream(file),response.getOutputStream());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 通过ajax调用但文件上传
     * @param file
     * @return
     */
    @RequestMapping(method = {RequestMethod.POST},value = "/file/image")
    @ResponseBody
    public ResponseEntity imageUpload(@RequestParam(name = "file") MultipartFile file){

        log.info("file-->{}",file);
        try {
            FileCopyUtils.copy(file.getBytes(),new FileOutputStream("/Users/devilmole/Documents/attachment/"+file.getOriginalFilename()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map result=new HashMap();
        result.put("result","done");
        result.put("url","/vote/getFile");
        result.put("filename",file.getOriginalFilename());
        return new ResponseEntity(JSONObject.toJSONString(result), HttpStatus.OK);
    }

        /**
     * 批量文件上传
     * @param request
     * @return
     */
    @RequestMapping(method = {RequestMethod.POST},value = "/file/files")
    @ResponseBody
    public String imagesUpload(MultipartHttpServletRequest request){

        Iterator<String> itr= request.getFileNames();

        MultipartFile mpf=null;

        while (itr.hasNext()){
            mpf=request.getFile(itr.next());
            log.info("file name-->",mpf.getOriginalFilename());
        }
        return "done";
    }
}
